const { LinValidator,Rule } = require('../../core/lin-validator')
const User = require('../models/user')
const { LoginType, ArtType } = require('../lib/enum.js')


function checkType(vals) {
  const type = vals.body.type || vals.path.type
  if(!type) {
    throw new Error('type是必传的')
  }

  if(vals.body.type) {
    this.parsed.body.type = parseInt(type)
  }else {
    this.parsed.path.type = parseInt(type)
  }
  
  if(!LoginType.isThisType(parseInt(type))) {
    throw new Error('type参数不合法')
  }
}

function checkArtType(vals) {
  const type = vals.body.type || vals.path.type
  if(!type) {
    throw new Error('type是必传的')
  }

  if(vals.body.type) {
    this.parsed.body.type = parseInt(type)
  }else {
    this.parsed.path.type = parseInt(type)
  }
  
  if(!ArtType.isThisType(parseInt(type))) {
    throw new Error('type参数不合法')
  }
}

// class Checker {
//   constructor(type) {
//     this.enumType = type
//   }
//   check(vals) {
//     const type = vals.body.type || vals.path.type
//     if(!type) {
//       throw new Error('type是必传的')
//     }
  
//     // if(vals.body.type) {
//     //   this.parsed.body.type = parseInt(type)
//     // }else {
//     //   this.parsed.path.type = parseInt(type)
//     // }
    
//     if(!this.enumType.isThisType(parseInt(type))) {
//       throw new Error('type参数不合法')
//     }
//   }
// }

class PositiveIntegerValidator extends LinValidator {
  constructor() {
    super()
    this.id = [
      new Rule('isInt', '需要时正整数', {min: 1})
    ]
  }
}

class RegisterValidate extends LinValidator {
  constructor() {
    super()
    this.email = [
      new Rule('isEmail', '邮箱不符合规范')
    ]
    this.password1 = [
      // 密码 用户制定范围 
      new Rule('isLength', '密码至少6位，最多为32位', {min: 6, max: 32}),
      new Rule('matches', '密码不符合规范', '^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]')
    ]
    this.password2 = this.password1
    this.nickname = [
      new Rule('isLength', '昵称长度不符合', {min: 2, max: 12})
    ]
  }

  validatePassword(vals) {
    const psw1 = vals.body.password1
    const psw2 = vals.body.password2
    if(psw1 !== psw2) {
      throw new Error('两个密码必须相同')
    }
  }

  async validateEmail(vals) {
    const email = vals.body.email
    const user = await User.findOne({
      where: {
        email: email
      }
    })
    if(user) {
      throw new Error('email已存在')
    }

  }
}


class TokenValidator extends LinValidator {
  constructor() {
    super()
    this.account = [
      new Rule('isLength', '不符合账号规则', {min: 4, max: 32})
    ]
    this.secret = [
      // 账号 + 密码 
      // account  
      new Rule('isOptional'),
      new Rule('isLength', '至少6个字符', { min: 6, max: 128})
    ]
    this.type = checkType
  }
  // validateLoginType(vals) {
  //   console.log('vals', vals)
  //   if(!vals.body.type) {
  //     throw new Error('type是必传的')
  //   }
  //   if(!LoginType.isThisType(vals.body.type)) {
  //     throw new Error('type参数不合法')
  //   }
  // }
}

class NotEmptyValidator extends LinValidator {
  constructor() {
    super()
    this.token = [
      new Rule('isLength', '不允许为空', {min: 1})
    ]
  }
}


class LikeValidator extends PositiveIntegerValidator {
  constructor() {
    super()
    // const checker = new Checker(ArtType)
    // this.validateType = checker.check.bind(checker)
    this.validateType = checkArtType
  }
}

class SearchValidator extends LinValidator {
  constructor() {
    super()
    this.q = [
      new Rule('isLength', '搜索关键词不能为空', {
        min:1,
        max: 16
      })
    ]
    this.start = [
      new Rule('isInt', 'start不符合规范', {min: 0}),
      new Rule('isOptional', '', 0)
    ]
    this.count = [
      new Rule('isInt', 'count不符合规范', {min: 1, max: 20}),
      new Rule('isOptional', '', 20)
    ]
  }
}

class AddShortCommentValidator extends LinValidator {
  constructor() {
    super()
    this.content = [
        new Rule('isLength', '必须在1到12个字符之间', {
            min: 1,
            max: 12
        })
    ]
  }
}

module.exports = {
  PositiveIntegerValidator,
  RegisterValidate,
  TokenValidator,
  NotEmptyValidator,
  LikeValidator,
  SearchValidator,
  AddShortCommentValidator
}